<?php

/**
 * 定时更新商城1/2级前后台分类关系
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\ShopCategoryClassify;


$relation_cates = [];
//获取所有前台一级分类
$front_first_cates = ShopCategoryClassify::selectInfos(['parent_id' => 0], 'id');
//获取前台二级分类
$front_second_cates = ShopCategoryClassify::selectNormalIdByPids($front_first_cates, [ShopCategoryClassify::STATUS_NORMAL]);

//获取前台一二级分类对应后台分类关系
if ($front_second_cates && is_array($front_second_cates)) {
    foreach ($front_second_cates as $front_first_cate => $second_cates) {
        $relation_cates[$front_first_cate] = ShopCategoryClassify::selectNormalIdByPids($second_cates, [ShopCategoryClassify::STATUS_NORMAL], true);
        if ($second_cates && is_array($second_cates)) {
            foreach ($second_cates as $second_cate) {
                $relation_cates[$second_cate] = ShopCategoryClassify::selectNormalIdByPids($second_cate, [ShopCategoryClassify::STATUS_NORMAL], true);
            }
        }
    }
}

//存入缓存
$redis = new \Hhz\Redis\newCache(\hhz\constant\redis\redisKeys::KEY_B_CACHE_CATEGORY_CLASSIFY_);
if ($relation_cates && is_array($relation_cates)) {
    foreach ($relation_cates as $front_cate => $behind_cates) {
        $redis->setCache(implode(',', $behind_cates),$front_cate);
    }
}
echo "已完成";

